home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9966 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  72 lines

  1. Newsgroups: comp.lang.c++
  2. Path: news.sprintlink.net!mv!usenet
  3. From: ENGR@GSSI.MV.COM (Michael Furman)
  4. Subject: Re: Saving a C++ object
  5. Message-ID: <DnJw65.AM3@mv.mv.com>
  6. Mime-Version: 1.0
  7. Organization: GSSI
  8. Date: Thu, 29 Feb 1996 18:46:52 GMT
  9. References: <3135C74E.446B9B3D@doc.ic.ac.uk>
  10. X-Newsreader: WinVN 0.93.10
  11. X-Nntp-Posting-Host: gssi.mv.com
  12.  
  13. In article <3135C74E.446B9B3D@doc.ic.ac.uk>, brj@doc.ic.ac.uk says...
  14. >
  15. >When I save a C++ object just using:
  16. >
  17. >----------------------------
  18. >class ovoid{
  19. >/* etc. */
  20. >}
  21. >
  22. >ovoid square
  23. >
  24. >file.write((unsigned char *) &square, sizeof(ovoid))
  25. >----------------------------
  26. >
  27. >What *exactly* is saved? I'm finding that every time I recompile
  28. >an application when I've added some new code, trying to load
  29. >an object saved in this way with an old version of the application
  30. >crashes it, even if the class of the object in question hasn't
  31. >changed. I presume there are some pointers or something which are
  32. >changing when I add new chunks of code. What can I do to avoid this?
  33. >
  34. >(ovoid doesn't containg any explicit pointers which are not reset when
  35. >reloading - ie. I'm not trying to use old pointers which have
  36. >been saved - not to my knowledge anyway)
  37.  
  38. Your class probably has virtual member functions. You can do such read / 
  39. write operations only for classes that mapped to memory as regular "C"
  40. structures. Such classes cannot have virtual functions and base classes.
  41.  
  42. >
  43. >Also, is there anything dodgy about saving a class from within itself, 
  44. >using:
  45. >
  46. >file.write((unsigned char *) &this, sizeof(ovoid))
  47.  
  48. Nothing except bug you made. "this" is a pointer to the class instance, not 
  49. the instance or reference. And your line must be:
  50.  
  51. file.write((char *)this, sizeof(ovoid));
  52.  
  53. And be sure that your stream (fine) onened in binary mode.
  54.  
  55.  
  56.  
  57. >
  58. >Thanks for your help.
  59. >
  60. >-- 
  61. >Je suis triste et seul ici.
  62.  
  63. -- 
  64. <<< If you received it by E-mail: it is a copy of post to the newsgroup >>>
  65. ---------------------------------------------------------------
  66. Michael Furman,                       (603)893-1109
  67. Geophysical Survey Systems, Inc.  fax:(603)889-3984
  68. 13 Klein Drive - P.O. Box 97          engr@gssi.mv.com 
  69. North Salem, NH 03073-0097            71543.1334@compuserve.com
  70. ---------------------------------------------------------------
  71.  
  72.